package com.oocl.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.pojo.Protocol;

public class HTTPConnUtil {
	
	private static final Logger LOGGER = Logger.getLogger(HTTPConnUtil.class);

	private static  boolean isRun = true; 
	
	public static String sendGet(String address) {
		HttpURLConnection hconn = null;
		InputStream in = null;
		String response = "";
		try {
			URL url = new URL(address);
			URLConnection conn = url.openConnection();
			hconn = (HttpURLConnection) conn;
			
			hconn.setRequestMethod("GET");
			
			in = hconn.getInputStream();
			int len = hconn.getContentLength();
			byte[] buffer = new byte[len];
			in.read(buffer);
			
			response = new String(buffer, "utf-8");
		} catch (IOException e) {
			LOGGER.info("", e);
			return null;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					LOGGER.debug("", e);
				}
			}
			if (hconn != null) {
				hconn.disconnect();
			}
		}
		
		LOGGER.info("response:"+response);
		return response;
	}

	public static void setRun(boolean isRun) {
		HTTPConnUtil.isRun = isRun;
	}

	public static boolean isRun() {
		return isRun;
	}

	
//	public static void main(String[] args) {
//		String s = sendGet("http://10.222.29.178:9090/O2OAdministrator/approvalResult&id=");
//		System.out.println(s);
//	}
}
